package client;

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;


public class LoginFrame extends JFrame implements ActionListener{
	
	private JTextField jtf = null;
	
	public LoginFrame(){
		try{
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch(Exception e){
			e.printStackTrace();
		}
		
		init();
	}
	
	public void init(){
		JPanel jp = new JPanel();
	    jtf = new JTextField();
		JLabel title = new JLabel("Welcome to Chat System.");
		JLabel jl = new JLabel("Name:");
		JButton jb1 = new JButton("OK");
		JButton jb2 = new JButton("Exit");
		getContentPane().setLayout(null);
		setPreferredSize(new Dimension(300, 250));
		getContentPane().add(jp);
		//getContentPane().add(title);
		jp.setLayout(null);
		jp.setBounds(0, 0, 300, 250);
		jp.add(title);
		title.setBounds(50, 30, 300, 50);
		title.setFont(new Font("", Font.BOLD, 16));
		//jp.setVisible(true);			
		jp.add(jl);
		jl.setBounds(30, 90, 50, 30);
		jp.add(jtf);
		jtf.setBounds(90, 90, 180, 40);
		jp.add(jb1);
		jb1.setBounds(100, 150, 60, 30);
		jb1.addActionListener(this);
		jp.add(jb2);
		jb2.setBounds(180, 150, 60, 30);
		jb2.addActionListener(this);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		pack();
		setLocation(400,200);
		setVisible(true);
	}
	
	public static void main(String[] args) {
		new LoginFrame();
	}
	
	public void actionPerformed(ActionEvent e) {
		String command = e.getActionCommand();
		
		if (command.equals("OK")) {
			String str = jtf.getText().trim();
			ChatClient cc = new ChatClient(str);
			if(cc.init())
				setVisible(false);
			else{
				JOptionPane.showMessageDialog(null, "It's failed to connetc to the server!", 
						"Error", JOptionPane.ERROR_MESSAGE);
			}
		}
		else if (command.equals("Exit")){
			setVisible(false);
			System.exit(0);
		}
	}

}
